﻿namespace Dcms.UpdateModule
{
    using Dcms.BLL;
    using Dcms.Common;
    using Dcms.Config;
    using System;
    using System.Collections;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web;

    public class DcmsCommSqlProcess
    {
        public static int CheckCurrPageIndex(string SqlStr, int CurrPageIndex, int PageSize, params object[] cmdParms)
        {
            int num = DcmsDataBaseHelper.GetDataCount(SqlStr, 0, cmdParms);
            if (num > CurrPageIndex * PageSize) return CurrPageIndex;
            if (num == 0) return 1;
            int num2 = num % PageSize;
            if (num2 > 0) return num / PageSize + 1;
            return num / PageSize;
        }

        public static string FarmatStringLenght(string RegexStr, string HtmlTempLate, string NewVlaue)
        {
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(HtmlTempLate);
            if (matchs.Count > 0)
            {
                Regex regex2 = new Regex(RegexStr, RegexOptions.IgnoreCase);
                foreach (Match match in matchs)
                {
                    HtmlTempLate = regex2.Replace(HtmlTempLate, FormatStrLength(NewVlaue, match.Groups["Len"].Value));
                }
            }
            return HtmlTempLate;
        }

        public static string FarmatTimeStr(string RegexStr, string HtmlTempLate, string NewVlaue)
        {
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(HtmlTempLate);
            if (matchs.Count > 0)
            {
                foreach (Match match in matchs)
                {
                    HtmlTempLate = Regex.Replace(HtmlTempLate, RegexStr, FormatDataTime(NewVlaue, match.Groups["Style"].Value), RegexOptions.IgnoreCase);
                }
            }
            return HtmlTempLate;
        }

        public static string FormatDataTime(string DataTimeStr, string StyleStr)
        {
            return Utils.FormatDataTime(DataTimeStr, StyleStr);
        }

        public static string FormatSql(string Value)
        {
            return Utils.check_TxtSql(Value);
        }

        public static string FormatStrLength(string Str, string LenghtStr)
        {
            Str = Utils.ClearHtml(Str);
            return Utils.cutString(Str, Utils.StrToInt(LenghtStr, 10));
        }

        public static string GetCateListInfo(string CateId, string SelectDir)
        {
            Dcms_CateBLL ebll = new Dcms_CateBLL();
            string[] strArray = CateId.Split(new char[] { ',' });
            string str = "";
            string langVerStr = "";
            langVerStr = GetLangVer();
            int cateType = 0;
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i] != "")
                {
                    Regex regex = new Regex("^[1-9][0-9]*$");
                    if (regex.Match(strArray[i]).Success) cateType = 1;
                    switch (SelectDir.ToLower())
                    {
                        case "sup":
                            str = str + ebll.GetSupCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "hor":
                            str = str + ebll.GetHorCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "sub":
                            str = str + ebll.GetSubCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "thisandsub":
                            str = str + ebll.GetThisAndSubCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "allsub":
                            str = str + ebll.GetAllSubCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "thisandallsub":
                            str = str + ebll.GetThisAndAllSubCateIdList(strArray[i], langVerStr, cateType) + ",";
                            break;

                        case "this":
                            str = str + ebll.GetThisCateId(strArray[i], langVerStr, cateType) + ",";
                            break;

                        default:
                            str = str + ebll.GetThisCateId(strArray[i], langVerStr, cateType) + ",";
                            break;
                    }
                }
            }
            if (str == "") return "0";
            if (str.LastIndexOf(',') == str.Length - 1) str = str.Substring(0, str.Length - 1);
            return str;
        }

        public static string GetLangVer()
        {
            WebSiteConfigInfo info;
            string str = "CN";
            string[] strArray = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '/' });
            if (strArray.Length >= 5)
            {
                if (strArray[4] == "")
                {
                    info = WebSiteConfigs.Deserialize("~/SystemConfig/WebSiteInfo.config");
                    if (info.defaultStyle != "") str = info.defaultStyle.ToUpper();
                    return str;
                }
                return strArray[4].ToUpper();
            }
            info = WebSiteConfigs.Deserialize("~/SystemConfig/WebSiteInfo.config");
            if (info.defaultStyle != "") str = info.defaultStyle.ToUpper();
            return str;
        }

        public static string GetModelName(string ConditionStr, string RegexStr, string StrName)
        {
            MatchCollection matchs = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(ConditionStr);
            if (matchs.Count > 0) return matchs[0].Groups[StrName].Value;
            return "";
        }

        public static ArrayList GetParaNameArr(string TempSqlStr, ref ArrayList ConstDefaultvalueArr)
        {
            string pattern = @"\[(?<ParaName>(\w+#?(\w,?)*?))\]";
            ArrayList list = new ArrayList();
            MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(TempSqlStr);
            if (matchs.Count > 0)
            {
                foreach (Match match in matchs)
                {
                    string[] strArray = match.Groups["ParaName"].Value.Split(new char[] { '#' });
                    list.Add(strArray[0]);
                    if (strArray.Length > 1)
                        ConstDefaultvalueArr.Add(strArray[1]);
                    else
                        ConstDefaultvalueArr.Add("0");
                }
            }
            return list;
        }

        public static ArrayList GetParaNameArr(string TempSqlStr, ref string ContstParaStr, ref ArrayList ConstDefaultvalueArr)
        {
            string pattern = @"\[(?<ParaName>(\w+,?\w*?))\]";
            string str2 = @"\((?<DefaultValue>(\w+,?\w*?))\)";
            ArrayList list = new ArrayList();
            MatchCollection matchs = new Regex(pattern, RegexOptions.IgnoreCase).Matches(TempSqlStr);
            if (matchs.Count > 0)
            {
                foreach (Match match in matchs)
                {
                    string[] strArray = match.Groups["ParaName"].Value.Split(new char[] { ',' });
                    list.Add(strArray[0]);
                    if (strArray.Length > 1)
                        ConstDefaultvalueArr.Add(strArray[1]);
                    else
                        ConstDefaultvalueArr.Add("0");
                }
            }
            MatchCollection matchs2 = new Regex(str2, RegexOptions.IgnoreCase).Matches(TempSqlStr);
            if (matchs2.Count > 0) ContstParaStr = matchs2[0].Groups["DefaultValue"].Value;
            return list;
        }

        public static DataTable ReturnTableData(string TempSqlStr, string PageSize, params object[] cmdParms)
        {
            DataTable table = new DataTable();
            int currPageIndex = 1;
            if (PageSize == "0") return DcmsDataBaseHelper.ExecuteTable(TempSqlStr, CommandType.Text, cmdParms);
            if (DcmsRequest.GetQueryInt("Page", 0) != 0) currPageIndex = Convert.ToInt32(HttpContext.Current.Request.QueryString["Page"].ToString());
            string sqlStr = "select Count(*) " + TempSqlStr.Substring(TempSqlStr.IndexOf("from", StringComparison.CurrentCultureIgnoreCase), TempSqlStr.Length - TempSqlStr.IndexOf("from", StringComparison.CurrentCultureIgnoreCase));
            sqlStr = sqlStr.Substring(0, sqlStr.LastIndexOf(" order ", StringComparison.CurrentCultureIgnoreCase));
            currPageIndex = CheckCurrPageIndex(sqlStr, currPageIndex, Convert.ToInt32(PageSize), cmdParms);
            Utils.WriteCookie("TotalRecord", DcmsDataBaseHelper.GetDataCount(sqlStr, 0, cmdParms).ToString());
            Utils.WriteCookie("CurrPageIndex", currPageIndex.ToString());
            Utils.WriteCookie("PageSize", PageSize);
            Utils.WriteCookie("RadPage", "5");
            return DcmsDataBaseHelper.DataTableList(TempSqlStr, CommandType.Text, Convert.ToInt32(PageSize), currPageIndex, cmdParms);
        }
    }
}

